/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gridworld_v04;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jonah
 */
public class Critter extends Actor {


    /*implementation comment*/
    ////<editor-fold defaultstate="collapsed" desc="Static Variables">
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Instanced Variables">
    private List<Tile> safeTiles = new ArrayList();
    private int lifeSpan;
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Constructor">
    public Critter(Tile currentTile, Game game, Board board, int lifeSpan) {
        super(currentTile, game, board);
        this.lifeSpan = lifeSpan;
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Class Methods">
    @Override public void checkSafeTiles() {
        safeTiles.clear();
        List<Tile> unsafeTiles = new ArrayList();
        
        loop:
            for (Tile t : currentTile.getNeighbors()){
                for (Actor o: t.getOccupants() ){
                    if (o instanceof Critter){
                        unsafeTiles.add(t);
                        //continue loop;
                    }else if(o instanceof Rock){
                        unsafeTiles.add(t);
                    }
                }
                safeTiles.add(t);
                //System.out.println("safe tile added");
            }
        //end loop:
        safeTiles.removeAll(unsafeTiles);
        if (safeTiles.size()<1){
            safeTiles.add(currentTile);
        }
    }
    
    private void checkBugTiles(){
        List<Tile> bugTiles = new ArrayList();
        int adjacentBugs = 0;
        for(Tile t: safeTiles){
            for ( Actor o : t.getOccupants()){
                if (o instanceof Bug){
                    adjacentBugs++;
                    bugTiles.add(t);
                }
            }
        }
        if (adjacentBugs > super.getGame().adjacentBugsToKillCritter){
            remove();
        }
        if (bugTiles.size()>0){
            safeTiles = bugTiles;
            this.lifeSpan += super.getGame().critterEatsBug;//we need to put this here
            //since unlike bug the food wont show up when we check the current tile
        }
    }
    
    @Override public void move() {
        Tile oldTile = currentTile;
        checkSafeTiles();
        checkBugTiles();
        moveToTile(safeTiles.get((int)(Math.random()*safeTiles.size())));
        if (updateLife()){
            this.lifeSpan -= super.getGame().critterBirth;
            super.getGame().createActorInTile(TileEnum.Critter, oldTile);
        }
        this.lifeSpan--;
    }
    
    private boolean updateLife(){
        if (this.lifeSpan < 1){
            System.out.println("Critter:"+this+" has died");
            super.remove();
            return false;
        }
        if (this.lifeSpan > super.getGame().critterReproductiveSpan){
            super.getGame().addActorReference(this);
            return true;
        }
        super.getGame().addActorReference(this);
        return false;
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Interface Override Methods">
    //</editor-fold>
}
